/**
 * Created by chengxiaolong on 2016/5/31.
 */
package com.invexer.service.linggang {
import com.invexer.service.common.InvexerHttpService;

public class LingGangHttpService implements ILingGangService {
    //private static var logger : Logger = //logger.getLogger(LingGangHttpService);

    private var _httpService:InvexerHttpService;
    private var _productionMode:String;
    private var _config:Object;

    public function LingGangHttpService() {
        _httpService = new InvexerHttpService();
    }

    public function getApi(key:String):String{
        return _config.baseUrl[_productionMode] + _config.apis[key];
    }

    public function set productionMode(value:String):void {
        _productionMode = value;
    }

    public function set config(value:Object):void {
        _config = value;
    }

    public function login(username:String, password:String, successCallback:Function, failCallback:Function):void {
        _httpService.sendGetRequest(getApi("login"), {username:username, password:password}, function (data:Object):void {
            data = processData(data);
            successCallback(data);
        }, failCallback, true);
    }

    public function getSupplierTagData(successCallback:Function, failCallback:Function):void {
        _httpService.sendGetRequest(getApi("getSupplierTagData"), null, function (data:Object):void {
            data = processData(data);
            successCallback(data ? data.supplierTaglist : null);
        }, failCallback);
    }

    public function getCustomData(successCallback:Function, failCallback:Function):void {
        _httpService.sendGetRequest(getApi("getCustomData"), null, function (data:Object):void {
            data = processData(data);
            successCallback(data ? data.customData : data);
        }, failCallback);
    }

    public function getSaleData(successCallback:Function, failCallback:Function):void {
        _httpService.sendGetRequest(getApi("getSaleData"), null, function (data:Object):void {
            data = processData(data);
            successCallback(data ? data.reportViewlist : null);
        }, failCallback);
    }

    public function getRegionData(steelType:String, successCallback:Function, failCallback:Function):void {
        _httpService.sendGetRequest(getApi("getRegionData"), {productGroupCode:steelType}, function (data:Object):void {
            data = processData(data);
            successCallback(data);
        }, failCallback);
    }

    public function getStockData(successCallback:Function, failCallback:Function):void {
        _httpService.sendGetRequest(getApi("getStockData"), null, function (data:Object):void {
            data = processData(data);
            successCallback(data ? data.stockData : data);
        }, failCallback);
    }

    public function getPriceData(period:int, successCallback:Function, failCallback:Function):void {
        _httpService.sendGetRequest(getApi("getPriceData"), { timetype:period }, function (data:Object):void {
                    data = processData(data);
                    successCallback(data);
                }, failCallback);
    }


    private function processData(data:Object):Object{
        var jsonString:String = data as String;
        if(!jsonString || jsonString.length==0) return null;

        data = JSON.parse(jsonString);

        return data;
    }
}
}
